package com.gitee.zeric.codec.util;

import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class TenantUtils {

    private static final String TENANT_ID = "TENANT-ID";


    /**
     *  返回租户id
     * @return
     */
    public static Integer getTenantId() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        final String tenantId = request.getHeader(TENANT_ID);
        return StrUtil.isNotBlank(tenantId) && !StrUtil.equals("null",tenantId) ? Integer.parseInt(tenantId) : 1;
    }



}
